//********************************************************
// Popup Listener.java 
// Date: 4/30/2012
// Author: Andrew Landphier
//********************************************************
// Popup listener which responds to right click mouse events. Creates a popup menu
// passing in the original source panel to the menu class. Popup listener only responds
// to enabled components.
//*******************************************************

package ui.listener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;

import driver.Driver;

import ui.Board;
import ui.PopupMenu;

public class PopupListener extends MouseAdapter{
	public PopupMenu popup;
	public JPanel source;
	
    public PopupListener() {
        
    }
    
	public void mousePressed(MouseEvent e){
		
		try {
			maybeShowPopup(e);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	
	}
	
	public void mouseReleased(MouseEvent e){
		
		
		try {
			maybeShowPopup(e);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
	}
	
	//right click is event trigger popup menu
	private void maybeShowPopup(MouseEvent e) throws InterruptedException{
		//Panel is disabled if it has had a tile placed
		if (e.getComponent().isEnabled()){
			if (e.isPopupTrigger()) {
				
				popup = new PopupMenu((JPanel) e.getSource());
				popup.initialize().show(e.getComponent(), e.getX(), e.getY());
				
			}
			
		}
	}
}
